package com.app.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.app.application.MyApplication;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.main.permission.PermissionUtil;
import com.lepu.app.main.permission.callback.PermissionResultCallBack;

import java.util.List;

/**
 * Created by wxd on 2016/11/1.
 */
public class LocationUtil implements LocationListener,BDLocationListener {

    private static LocationUtil mInstance = null;

    public float mAccuracy = 0f; // 精确度
    public double mAltitude = 0d; // 海拔
    public double mLatitude = 0d; // 纬度
    public double mLongitude = 0d; // 经度

    private LocationUtil() {

    }

    public static LocationUtil getInstance() {
        if (mInstance == null) {
            mInstance = new LocationUtil();
        }
        return mInstance;
    }

    public void getLocationSystem(final Activity context) {
        //获取定位管理对象
        try {
            LocationManager lm = (LocationManager) MyApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);
            List<String> names = lm.getAllProviders();//获取所有的位置提供者，一般三种

            Criteria criteria = new Criteria();//查询条件，如果设置了海拔，则定位方式只能是GPS;
            criteria.setCostAllowed(true);//是否产生开销，比如流量费
            String provider = lm.getBestProvider(criteria, true);//获取最好的位置提供者，第二个参数为true，表示只获取那些被打开的位置提供者
            lm.requestLocationUpdates(provider, 0l, 0f, LocationUtil.this);
            //获取位置。第二个参数表示每隔多少时间返回一次数据，第三个参数表示被定位的物体移动每次多少米返回一次数据。

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getLocationBaidu(final Activity context) {
        //获取定位管理对象
        try {
            // 定位初始化
            LocationClient locClient = new LocationClient(context);
            locClient.registerLocationListener(LocationUtil.this);
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            //option.setScanSpan(60000); //一分钟定位一次
            option.setAddrType("all"); //获取string 地址 4.0之前
            option.setIsNeedAddress(true); //获取string 地址4.0之后

            locClient.setLocOption(option);
            locClient.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
            double longitude = location.getLongitude(); // 经度
            double latitude = location.getLatitude(); // 纬度

            if(longitude <= 180 && latitude <= 90){
                mLongitude = longitude;
                mLatitude = latitude;
                mAccuracy = location.getAccuracy(); // 精确度 多少米
                mAltitude = location.getAltitude(); // 海拔
            }
        }

        MyApplication.getInstance().mGetSuccessLocation = true;
        Utils.firstUpload();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        LogUtilBase.LogD(null, "LocationUtil onStatusChanged");
    }

    @Override
    public void onProviderEnabled(String provider) {
        LogUtilBase.LogD(null, "LocationUtil onProviderEnabled");
    }

    @Override
    public void onProviderDisabled(String provider) {
        LogUtilBase.LogD(null, "LocationUtil onProviderDisabled");
    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {

        if(bdLocation != null)
        {
            //LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
            double longitude = bdLocation.getLongitude(); // 经度
            double latitude = bdLocation.getLatitude(); // 纬度

            if(longitude <= 180 && latitude <= 90){
                mLongitude = longitude;
                mLatitude = latitude;
                //mAccuracy = bdLocation.getAccuracy(); // 精确度 多少米
                mAltitude = bdLocation.getAltitude(); // 海拔
            }
        }

        MyApplication.getInstance().mGetSuccessLocation = true;
        Utils.firstUpload();
    }
}
